using System;
//using System.ComponentModel;
using System.Drawing;
//using System.Drawing.Drawing2D;

using Camalon.Drawing.Shapes;
//using System.Windows.Forms;

using System.Drawing.Imaging;
using System.IO;

namespace Camalon.Drawing
{
	/// <summary>
	/// Summary description for CodeTestClass.
	/// </summary>
	public class CodeTestClass
	{
		public CodeTestClass()
		{
			//
			// TODO: Add constructor logic here
			//
		}

//		public static void AnchorPoint( Line line , int index )
//		{
//			//Get the anchor points. The Points property return a copy of the array.
//			AnchorPoint[] anchorPoints = line.Points;
//			//Get a copy of anchor point at the specified index
//			AnchorPoint anchorPoint = anchorPoints[index];
//			//Set the anchor style
//			anchorPoint.Anchor = AnchorStyles.Top|AnchorStyles.Right;
//			//Set modified anchor point at the specified index
//			anchorPoints[index] = anchorPoint;
//			//Reset the modified anchor points array in the line. This will automatically invalidate the shape.
//			line.Points = anchorPoints;
//		}

		public static void PrintToBitmap( string fileName, Shape shape )
		{

			//Get the visible bounds of the shape to be painted.
			//InvalidatingBounds property on a Shape Container returns the union of painting bounds of all shapes
			Rectangle picBounds = Rectangle.Ceiling( shape.InvalidatingBounds );

			//Create a bitmap to paint and create a Graphics object based on the image
			using(Bitmap bmp = new Bitmap( picBounds.Width, picBounds.Height))
			using(Graphics g = Graphics.FromImage( bmp ))
			{

				//Fill the background outside 
				//of the shape's clipping region,which can also be visible 
				g.FillRectangle( Brushes.White, Rectangle.FromLTRB(0,0,picBounds.Width, picBounds.Height) );

				//Translate the painting region to image bounds
				int dx = picBounds.X < 0 ? 0: -picBounds.X;
				int dy = picBounds.Y < 0 ? 0: -picBounds.Y;
				g.TranslateTransform( dx, dy);

				//Paint shape to image
				shape.Paint( g );

				string ext = System.IO.Path.GetExtension(fileName);

				//Default image format is bitmap
				ImageFormat format = ImageFormat.Bmp;

				//Otherwise select the image format
				if( string.Compare( ext, ".png", true) == 0) format = ImageFormat.Png;
				else if( string.Compare( ext, ".jpeg", true) == 0) format = ImageFormat.Jpeg;
				else if( string.Compare( ext, ".tiff", true) == 0) format = ImageFormat.Tiff;

				//Save image 
				bmp.Save( fileName, format);

			}

		}
		public static void WarpShape( Shape shape )
		{
			try
			{
				shape.SuspendLayout();

				//Set shape bounds
				shape.Position = new Point( 100,100);
				shape.Size = new Size( 200, 200 );

				//Create a new warp
				Warp warp = Warp.Empty;
			
				//Set warp mode to perspective
				warp.WarpMode = System.Drawing.Drawing2D.WarpMode.Perspective;
			
				//Move TopLeft warp point to position (X:50;Y:50)
				warp.TopLeft	= new Point( -50,-50 );
			
				//Move TopRight warp point to position (X:350;Y:50)
				warp.TopRight	= new Point( 50,-50);
			
				//Move BottomLeft warp point to position (X:50;Y:350)
				warp.BottomLeft = new Point( -50,50);
			
				//Move BottomRight warp point to position (X:350;Y:250)
				warp.BottomRight = new Point( 50,-50);

				//Warp the shape
				shape.Warp = warp;
			}
			finally
			{
				shape.ResumeLayout();
			}
		
		}
	

		public static void ShearShape( Shape shape )
		{
			//Initialize and empty shear
			Shear shear = Shear.Empty;
			//Shear by 50 pixel in -X direction
			shear.X = -50;
			//Shear by 30 pixel in +Y direction
			shear.Y = 30;
			//Shear after rotation
			shear.AppendRotate = true;
			//Set shearing value 
			shape.Shear = shear;
		}

		public static void ScaleShape( Shape shape )
		{

			//Initialize and empty shear
			Scale scale = Scale.Empty;

			//Stretch  horizontally by 60 pixel
			scale.X = 60;

			//Shrink vertically by 30 pixel
			scale.Y = -30;

			//Scale after rotation
			scale.AppendRotate = true;

			//Set scaling value 
			shape.Scale = scale;

		}

		public static void CombineShapes( Shape shape, Shape combineWith )
		{

			//Initialize a shape combination using ShapeCombine structure
			ShapeCombine combine = new ShapeCombine( combineWith, ShapeCombineMode.Union);

			//Combine both shapes
			shape.Combine = combine;


		}

		public static void CreateCompoundLine( Line line )
		{
			try
			{
				line.SuspendLayout();

				//Set the points of the line to be drawn
				line.Points = new AnchorPoint[] { new AnchorPoint(400, 80),
													new AnchorPoint(290, 240),
													new AnchorPoint(200, 100),
													new AnchorPoint(70, 230)};

				//Specify a Linear Gradient Brush to paint the line
				LinearGradientFill linearGradientFill = new LinearGradientFill();
				linearGradientFill.InterpolationColors = new Camalon.Drawing.CompoundColor[] 
				{
					new Camalon.Drawing.CompoundColor(System.Drawing.SystemColors.InactiveCaption),
					new Camalon.Drawing.CompoundColor(System.Drawing.Color.IndianRed, 0.5f),
					new Camalon.Drawing.CompoundColor(System.Drawing.Color.DarkOrange, 1.0f)
				};

				//Create a new ShapePen object
				ShapePen shapePen = new ShapePen();

				//Define the compounds of the line
				shapePen.Compounds = new int[] {0,30,50,100};
				
				//Specify StartCap
				shapePen.StartCap = new Camalon.Drawing.LineCapStyle(
					Camalon.Drawing.CapStyle.Arrow, 
					Camalon.Drawing.CapSize.Small);

				//Specify End Cap
				shapePen.EndCap = new Camalon.Drawing.LineCapStyle(	
					Camalon.Drawing.CapStyle.Round, 
					Camalon.Drawing.CapSize.SmallSmall, 
					Camalon.Drawing.CapAlign.Inset, 
					0f, 
					false);

				shapePen.Width = 15.0f;
				shapePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
				shapePen.Fill = linearGradientFill;
				line.Pen = shapePen;

			}
			finally
			{
				line.ResumeLayout();
			}

		}							


		public static void DecorateShapeText( Shape shape )
		{
			shape.SuspendLayout();

			try
			{
				Camalon.Drawing.ShapePen textPen	= new ShapePen();
				Camalon.Drawing.SolidFill textFill	= new SolidFill()	;
				Camalon.Drawing.Shade textShade		= new Shade();
				Camalon.Drawing.SolidFill shadeFill = new SolidFill();
				Camalon.Drawing.ShapePen rectPen	= new ShapePen();

				// textFill
				textFill.Color = new Camalon.Drawing.TransparentColor(System.Drawing.Color.IndianRed);

				// textShade
				textShade.Fill = shadeFill;
				textShade.Offset = new System.Drawing.Point(5, 5);

				// shadeFill
				shadeFill.Color = new Camalon.Drawing.TransparentColor(System.Drawing.Color.LightGray);

				// rectPen
				rectPen.Width = 2F;

				// shape
				shape.Size = new System.Drawing.Size(70, 320);
				shape.Pen = rectPen;
				shape.Font = new System.Drawing.Font("Arial Rounded MT Bold", 48F);

				shape.Text = "Your Text";
				shape.TextFill = textFill;
				shape.TextFormat.TextDirection = Camalon.Drawing.TextDirection.VerticalBottomToTop;
				shape.TextFormat.Trimming = System.Drawing.StringTrimming.EllipsisCharacter;
				shape.TextPen = textPen;
				shape.TextShade = textShade;

				//Apply a Warp transformation to shape. 
				//If you dont't want your text to be transformed, set Transform property false. 
				//shape.TextFormat.Transform = false;
				shape.Warp = new Camalon.Drawing.Warp(
					new System.Drawing.Point(0, 0), 
					new System.Drawing.Point(140, 0), 
					new System.Drawing.Point(0, 0), 
					new System.Drawing.Point(0, 0));


			}
			finally
			{
				shape.ResumeLayout();
			}
		}
		

		public static void CreateCustomDashLine( Line line )
		{
			// linePen
			ShapePen linePen = new ShapePen();
			linePen.Color = new TransparentColor(System.Drawing.Color.Red);
			linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
			linePen.DashPattern = new int[] { 5,5,10,20};
			linePen.Width = 15.0f;

			//line
			line.Points = new AnchorPoint[] {new AnchorPoint(50, 50),new AnchorPoint(270, 50)};
			line.Pen = linePen;
		}

//		public static void FillShape( Shape shape )
//		{
//
//			//solid fill
//			SolidFill fill = new SolidFill( );
//			fill.Color = new TransparentColor( System.Drawing.Color.Red );
//
//			//shape
//			shape.Position = new Point(20, 20);
//			shape.Size = new Size( 200,100);
//			shape.Fill = fill;
//
//		}

		public static void FillShapePath( Shape shape )
		{

			//solid fill
			PathGradientFill fill = new PathGradientFill( );
			fill.InterpolationColors = new CompoundColor[] 
			{
				new CompoundColor(System.Drawing.Color.DarkGreen),
				new CompoundColor(System.Drawing.Color.Aqua, 0.25F),
				new CompoundColor(System.Drawing.Color.Blue, 1F)
			};

			//shape
			shape.Position = new Point(40, 40);
			shape.Size = new Size( 200,100);
			shape.Fill = fill;

		}
		public static void FillShapeLinear( Shape shape )
		{

			//Linear Gradient Fill
			LinearGradientFill fill = new LinearGradientFill( );


			//Green color starts on the left side (0% = 0F) of the shapes boundary
			//Aqua color is 50 percent (=0.5F) of the way from the left side of the shape's boundary.
			//Blue color ends on the right side (100% = 1F) of the shapes boundary
			fill.InterpolationColors = new CompoundColor[]
{

	new CompoundColor(System.Drawing.Color.Green, 0F),
	new CompoundColor(System.Drawing.Color.Aqua, 0.25F),
	new CompoundColor(System.Drawing.Color.Blue, 1F)

};


			//shape
			shape.Position = new Point(40, 40);
			shape.Size = new Size( 200,100);

			//To rotate only the shape fill and not the shape itself (Figure 2).
			//fill.Rotation = 45;

			//Enable gamma correction on the linear gradient fill (Figure 3).
			fill.GammaCorrection = true;

			//To allow transformation of the shape fill accordingly , when shape is transformed.
			//This overrides the [Rotate] property.(Figure 5)
			fill.Transform = true;

			//Tile the shape fill relative( Figure 6.2 ) and flip (alternating) horizontally. 
			fill.Tile = new FillTile(20,25);
			fill.WrapMode = System.Drawing.Drawing2D.WrapMode.TileFlipX;


			//Set the intensity(Figure 7).Intensity can only be applied to a 2-Color gradient.In this case,
			//the blue color in the will be ignored. We set the color in 20 percent distance 
			//along the gradient line, consists of 40 percent green and 60 percent aqua. 
			fill.Intensity = new ColorIntensity[]{ new ColorIntensity( 0.2F, 0.4F ) };


			//To transform the shape as shown in the Figures 4 and 5.To enable fill
			//transformation, you must set [Transform] property of LinearGradientFill to true 
			shape.Rotation = 16;
			shape.Scale = new Scale(-65, 0, true);
			shape.Shear = new Shear( -9, 46 );

			shape.Fill = fill;
		}


		public static void ShadeShape( Shape shape )
		{

			shape.SuspendLayout();


			try
			{

				Shade shade = new Shade();
				ShapePen shadePen = new ShapePen();
				PathGradientFill shadeFill = new PathGradientFill( );
   

				//shadeFill
				//We create a 2-Color gradient
				shadeFill.InterpolationColors = new CompoundColor[]
				{

					new CompoundColor(System.Drawing.Color.FromArgb(250,250,250)),
					new CompoundColor(System.Drawing.Color.Gray, 1F)

				};
				//Widen the focus 60% vertically and horizontally
				shadeFill.FocusScale = new SizeScale( 0.6F, 0.6F);


				//shadePen
				shadePen.Color = new TransparentColor(System.Drawing.Color.FromArgb(240,240,240));

				//shade
				shade.Fill = shadeFill;
				shade.Pen = shadePen;
				shade.Offset = new System.Drawing.Point(10,10);
				//Repeat shade more than ones
				//shade.Repeat = 2;


				//shape
				shape.Position = new Point(40, 30);
				shape.Size = new Size( 180,90);
				shape.Shade = shade;

			}
			finally
			{

				shape.ResumeLayout();

			}

		}

	}
}
